﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using System.Collections;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using QuangNgai.LTQNDataSetTableAdapters;

namespace QuangNgai.nhaplieu
{
    public partial class frmNhapHoSo : DevExpress.XtraEditors.XtraForm
    {

        public GridView GvChiTiet
        {
            get { return gvChiTiet; }
        }

        public GridControl GcHoSo
        {
            get { return gcHoSo; }
        }

        public GridView GvVanBan
        {
            get { return gvVanBan; }
        }

        public SimpleButton BtnThemHS
        {
            get { return btnThemHS; }
        }

        private ArrayList danhSachLienKetTuDien;
        public frmNhapHoSo()
        {
            InitializeComponent();
            danhSachLienKetTuDien = new ArrayList();
        }

        private void frmNhapHoSo_Load(object sender, EventArgs e)
        {
            this.vbbooksTableAdapter.Fill(this.lTQNDataSet.Vbbooks);
            this.dictionaryTableAdapter.Fill(this.lTQNDataSet.Dictionary);
            this.hsfieldsTableAdapter.Fill(this.lTQNDataSet.Hsfields);
            this.hsbooksTableAdapter.Fill(this.lTQNDataSet.Hsbooks);
            this.hoSoTableAdapter.Fill(this.lTQNDataSet.HoSo);
            hoSoDetailAdap.Fill(lTQNDataSet.HoSoDetail);
            vanBanAdap.Fill(lTQNDataSet.VanBan);
        }

        private void btnThemHS_Click(object sender, EventArgs e)
        {
            // neu chua chon loai tai lieu thi bao loi
            if (txtLoaiTaiLieu.EditValue == null)
            {
                MessageBox.Show("Chưa chọn loại tài liệu", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            LTQNLinQDataContext db = new LTQNLinQDataContext();

            //tao ho so moi
            HoSo hoso = new HoSo();
            hoso.Hsbooks_Id = Convert.ToInt32(txtLoaiTaiLieu.EditValue);
            hoso.Description = txtGhiChu.Text;

            var dsTruongTin = from att in db.Hsfieldsattributes
                              where att.Hsbooks_Id == hoso.Hsbooks_Id
                              select att;

            db.HoSos.InsertOnSubmit(hoso);
            // cap nhat ho so moi vao db
            db.SubmitChanges();

            // tao danh sach truong tin 
            foreach (var ttin in dsTruongTin)
            {
                HoSoDetail detail = new HoSoDetail();
                detail.HSID = hoso.HSID;
                detail.Hsfields_Id = ttin.Hsfields_Id;
                hoso.HoSoDetails.Add(detail);
            }
            // cap nhat vao db
            db.SubmitChanges();
            // load lai grid
            hoSoTableAdapter.Fill(this.lTQNDataSet.HoSo);
            hoSoDetailAdap.Fill(lTQNDataSet.HoSoDetail);

        }



        private void gvChiTiet_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            Console.Write("click ");
            /*ColumnView view = sender as ColumnView;
            LTQNDataSet.HoSoDetailRow currDetailRow = view.GetDataRow(e.RowHandle) as LTQNDataSet.HoSoDetailRow;
            LTQNLinQDataContext db = new LTQNLinQDataContext();

            int hsbookid = currDetailRow.HoSoRow.Hsbooks_Id;
            int hsfieldid = currDetailRow.Hsfields_Id;
            var hsfieldatt = from att in db.Hsfieldsattributes
                             where att.Hsbooks_Id == hsbookid
                             && att.Hsfields_Id == hsfieldid
                             select att;
            Hsfieldsattribute attribute = hsfieldatt.First();
            if (attribute.Linkdirectory!=null && attribute.Linkdirectory>0)
            {
                danhSachLienKetTuDien.Clear();
                dictionaryBindingSource.DataSource = getDicByLinkDictionary((int)attribute.Linkdirectory);
                Console.WriteLine("ok"); 
            }*/
            //this.dictionaryTableAdapter.Fill(this.lTQNDataSet.Dictionary);                 
            //dictionaryBindingSource.DataSource
        }

        /*private ArrayList getDicByLinkDictionary(int id)
        {
            
            LTQNLinQDataContext db = new LTQNLinQDataContext();
            var l = from d in db.Dictionaries
                    where d.ParentID == id
                    select d;
            foreach (var dic in l)
            {
                danhSachLienKetTuDien.Add(dic);
                getDicByLinkDictionary(dic.ID);
            }
            return danhSachLienKetTuDien;
        }*/

        private void leLKTD_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmNhapHoSoLKTD f = new frmNhapHoSoLKTD(this);
            f.Show();
        }

        private void gvChiTiet_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            ColumnView view = sender as ColumnView;
            //GridColumn col = view.Columns["AttributeValue"];
            //object bookName = view.GetRowCellValue(e.FocusedRowHandle, col);
            var currRow = view.GetFocusedDataRow() as LTQNDataSet.HoSoDetailRow;
            gvChiTiet.Tag = currRow;
        }

        public void gvChiTiet_RowUpdated(object sender, RowObjectEventArgs e)
        {
            hoSoBindingSource.EndEdit();
            hoSoDetailAdap.Update(lTQNDataSet.HoSoDetail);
        }

        public void gvChiTiet_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnThemHS.Focus();
                gcHoSo.Focus();
                gvChiTiet_RowUpdated(null, null);
            }
        }

        private void gvHoSo_RowUpdated(object sender, RowObjectEventArgs e)
        {
            hoSoBindingSource.EndEdit();
            hoSoTableAdapter.Update(lTQNDataSet.HoSo);
        }

        private void gvHoSo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnThemHS.Focus();
                gcHoSo.Focus();
                gvHoSo_RowUpdated(null, null);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            LTQNDataSet.HoSoRow hs = (hoSoBindingSource.Current as DataRowView).Row as LTQNDataSet.HoSoRow;
            string msg = "Bạn có muốn xóa HỒ SƠ này?\n" +
                hs.HSID + " - " + hs.HsbooksRow.Bookname;

            LTQNLinQDataContext db = new LTQNLinQDataContext();
            HoSo hoSo = (from h in db.HoSos where h.HSID == hs.HSID select h).First();

            if (MessageBox.Show(msg, "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
              DialogResult.Yes)
            {
                // OK: xoa ho so:
                // 1 - Ho so
                // 2 - Ho so details
                // 3 - Van ban ung voi ho so
                // 4 - Van ban file record
                // 5 - Van ban file details
                // 6 - File trong thu muc
                try
                {
                    _formUtil.DeleteHoSoFile(hoSo);
                    hoSoBindingSource.RemoveCurrent();
                    hoSoTableAdapter.Update(lTQNDataSet.HoSo);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void gvVanBan_RowUpdated(object sender, RowObjectEventArgs e)
        {
            hoSoBindingSource.EndEdit();
            vanBanAdap.Update(lTQNDataSet.VanBan);
        }

        private void gvVanBan_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnThemHS.Focus();
                gcHoSo.Focus();
                gvVanBan_RowUpdated(null, null);
            }
        }

        private void btnThemVB_Click(object sender, EventArgs e)
        {
            // neu chua chon loai tai lieu thi bao loi
            if (txtMaHS.EditValue == null)
            {
                MessageBox.Show("Chưa chọn hồ sơ", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (txtLoaiVB.EditValue == null)
            {
                MessageBox.Show("Chưa chọn loại văn bản", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            LTQNLinQDataContext db = new LTQNLinQDataContext();

            //tao ho so moi
            VanBan vanBan = new VanBan();
            vanBan.HSID = Convert.ToInt32(txtMaHS.EditValue);
            vanBan.Vbbooks_Id = Convert.ToInt32(txtLoaiVB.EditValue);
            vanBan.KHVB = txtKHVB.Text.Trim();
            vanBan.Description = txtGhiChuVB.Text.Trim();

            var dsTruongTin = from vbatt in db.Vbfieldsattributes
                              where vbatt.Vbbooks_Id == vanBan.Vbbooks_Id
                              select vbatt;

            db.VanBans.InsertOnSubmit(vanBan);
            // cap nhat ho so moi vao db
            db.SubmitChanges();

            foreach (var ttin in dsTruongTin)
            {
                VanBanDetail detail = new VanBanDetail();
                detail.VBID = vanBan.VBID;
                detail.Vbfields_Id = ttin.Vbfields_Id;
                vanBan.VanBanDetails.Add(detail);
            }
            // cap nhat vao db
            db.SubmitChanges();
            // load lai grid
            //hoSoTableAdapter.Fill(this.lTQNDataSet.HoSo);
            vanBanAdap.Fill(lTQNDataSet.VanBan);
            //hoSoDetailAdap.Fill(lTQNDataSet.HoSoDetail);
            VanBanDetailTableAdapter vbdetailadap = new VanBanDetailTableAdapter();
            vbdetailadap.Fill(lTQNDataSet.VanBanDetail);
            gvHoSo.CollapseAllDetails();
            gvHoSo.ExpandMasterRow(gvHoSo.FocusedRowHandle);
        }

        private void btnXoaVB_Click(object sender, EventArgs e)
        {
            //LTQNDataSet.HoSoRow hs = (hoSoBindingSource.Current as DataRowView).Row as LTQNDataSet.HoSoRow;
            //string msg = "Bạn có muốn xóa loại trường tin hồ sơ này?\n" +
            //    hs.HSID + " - " + hs.HsbooksRow.Bookname;
            //if (MessageBox.Show(msg, "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
            //  DialogResult.Yes)
            //{
            //    hoSoBindingSource.RemoveCurrent();
            //    hoSoTableAdapter.Update(lTQNDataSet.HoSo);
            //}
            var vb = gvVanBan.Tag as LTQNDataSet.VanBanRow;
            if (vb == null)
            {
                MessageBox.Show("Chưa chọn văn bản", "Lỗi nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string msg = "Bạn có muốn xóa văn bản " + vb.VBID;
            if (MessageBox.Show(msg, "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
              DialogResult.Yes)
            {
                vanBanAdap.DeleteByVBID(vb.VBID);
                vanBanAdap.Fill(lTQNDataSet.VanBan);
            }
        }

        private void gvVanBan_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            ColumnView view = sender as ColumnView;
            //GridColumn col = view.Columns["AttributeValue"];
            //object bookName = view.GetRowCellValue(e.FocusedRowHandle, col);
            var currRow = view.GetFocusedDataRow() as LTQNDataSet.VanBanRow;
            gvVanBan.Tag = currRow;
        }

        private void btnVBDetail_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var currRow = gvVanBan.Tag as LTQNDataSet.VanBanRow;
            frmChiTietVB f = new frmChiTietVB(currRow);
            f.ShowDialog();
        }
    }
}